package com.yidu.best.bond.bondMarket.service.impl;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import com.yidu.best.basicSetup.exchange.util.IDUtil;
import com.yidu.best.bond.bondMarket.dao.BondMarketDao;
import com.yidu.best.bond.bondMarket.domain.BondMarket;
import com.yidu.best.bond.bondMarket.domain.BondMarketVo;
import com.yidu.best.bond.bondMarket.service.BondMarketService;
import com.yidu.best.util.ExcelUtils;

@Service
public class BondMarketServiceImpl implements BondMarketService {
	@Autowired
	private BondMarketDao bondMarketDao;

	@Override
	public Map<String, Object> findPageQuery(BondMarketVo bondMarketVo) {
		// 调用dao层方法查询数量
		int count = bondMarketDao.findCountBondMarket(bondMarketVo);
		// 计算页码
		int page = (bondMarketVo.getPage() - 1) * bondMarketVo.getLimit();
		bondMarketVo.setPage(page);
		// 调用dao层方法分页查询
		List<BondMarket> bondMarketList = bondMarketDao.findPageQuery(bondMarketVo);
		Map<String, Object> map = new HashMap<String, Object>();
		// 将数据存入map中
		map.put("code", 0);
		map.put("count", count);
		map.put("data", bondMarketList);
		return map;
	}

	// @Transactional
	@Override
	public int insertBondMarket(MultipartFile file) throws IOException {
		File file2 = ExcelUtils.MultipartFileChangeFile(file);
		List<List<Object>> readExcel = ExcelUtils.readExcel(file2);
		// 数据集合
		List<BondMarket> dataList = new ArrayList<BondMarket>();
		int rs = 0;
		for (int i = 1; i < readExcel.size(); i++) {
			List<Object> objectsd = readExcel.get(i);
			BondMarket bondMarket = new BondMarket();
			bondMarket.setBondQuotationId(IDUtil.getUUID());
			bondMarket.setBondId("830c04c67675b1c4");
			// 设置债券代码
			bondMarket.setBondCode(String.valueOf(objectsd.get(0)));
			// 设置债券行情名
			bondMarket.setBondQuotationName(String.valueOf(objectsd.get(1)));
			// 设置涨幅
			bondMarket.setIncrease(new BigDecimal(objectsd.get(2).toString()));

			// 收盘
			bondMarket.setClosingPrice(new BigDecimal(objectsd.get(3).toString()));
			// 开盘价
			bondMarket.setOpeningPrice(new BigDecimal(objectsd.get(4).toString()));
			// 设置涨跌
			bondMarket.setRaisAndFall(new BigDecimal(objectsd.get(5).toString()));
			bondMarket.setTradeDate(new Date());
			// 将对象的数据赋值到数据集合
			dataList.add(bondMarket);
		}
		// 调用业务方法
		rs = bondMarketDao.insertBondMarket(dataList);

		return rs;
	}

}
